package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ljxt.dt.service.storemanagementservice.DetailService;
import com.ljxt.dt.service.storemanagementservice.RepertoryService;
import com.ljxt.dt.service.storemanagementservice.impl.DetailServiceImpl;
import com.ljxt.dt.service.storemanagementservice.impl.RepertoryServiceImpl;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.LastSelectObj;
import com.ljxt.dt.vo.storemanagement.ReceptVO;
import com.ljxt.dt.vo.storemanagement.RepertoryVO;
import com.ljxt.dt.vo.storemanagement.StockVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-20 16:55
 * @copyright 老九学堂
 */
@WebServlet("/lastDetail")
public class LastDetail extends HttpServlet {
    private RepertoryService admin  = new RepertoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到物品ID
       String datas = req.getParameter("datas");
      //通过第三方的gson将传入进来的json字符串转为对应的对象

        Gson gson = new Gson();
        RepertoryVO  repertoryVO = gson.fromJson(datas,RepertoryVO.class);
        req.setAttribute("data",repertoryVO);
        //再将这个对象转为json储存在req
        ObjectMapper objectMapper = new ObjectMapper();
        req.setAttribute("jsonData",objectMapper.writeValueAsString(repertoryVO));
        ServletUtil.forward(req,resp,"lastDetail","storeManagement");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取到商品属性
        Integer pageIndex = ServletUtil.getQueryInt(req,"pageIndex");
        Integer pageSize = ServletUtil.getQueryInt(req,"pageSize");
        Integer id = ServletUtil.getQueryInt(req,"id");

        if(pageIndex == 0){
            pageIndex = 1;
        }
        if(pageSize ==0){
            pageSize = 5;
        }

        PageVO pageVO = admin.getStocks(pageIndex,pageSize,id);

        pageVO.setPageURL("lastDetail");
        LastSelectObj lastSelectObj = new LastSelectObj();
        lastSelectObj.setId(id);
        pageVO.setSelectObj(lastSelectObj);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(pageVO);

        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().write(objectMapper.writeValueAsString(jsonResult));

    }
}
